﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Web;

namespace SmartSpace.News.UI.Parts
{
    public partial class ArticleList : SsPart
    {
        public ArticleList()
            : base(new Guid("{6B58DE18-7939-4A4D-B008-364C0423A023}"))
        {

        }

        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLCategory();
                BindList();
            }
        }

        private void BindDDLCategory()
        {
            NewsService searcher = new NewsService();
            _ctrlDDLCategory.DataSource = searcher.GetCategories().Data;
            _ctrlDDLCategory.DataTextField = "Name";
            _ctrlDDLCategory.DataValueField = "Id";
            _ctrlDDLCategory.DataBind();
            _ctrlDDLCategory.Items.Insert(0, new ListItem("请选择...", ""));
        }

        protected void _ctrlDDLCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_ctrlDDLCategory.SelectedIndex > 0)
            {
                BindList();
            }
        }

        private void BindList()
        {
            NewsSearcher searcher = new NewsSearcher();
            _ctlList.DataSource = searcher.FindArticles(Guid.Parse("BC61B173-84D6-489A-AA3D-CAB74A3B8BBC"), 0, 20).Data;
            _ctlList.DataBind();
        }

        protected void _ctlList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Guid articleID = new Guid(e.CommandArgument.ToString());
            if (e.CommandName == "Delete")
            {
                NewsService service = new NewsService();
                service.DeleteArticle(articleID);
            }
        }
    }
}